/*
 *   Copyright 1999-2016 Asiainfo Technologies(China),Inc.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */

/**
 * Created by xuwz on 2016/4/22.
 */

var Alert = (function() {
    "use strict";

    var elem,
        hideHandler,
        that = {};

    that.init = function(options) {
        elem = $(options.selector);
    };

    that.show = function(text) {
        clearTimeout(hideHandler);
        if (!elem){
            elem = $(".bb-alert");
            if (elem.length == 0){
                var alertDiv = $("<div></div>").addClass("bb-alert alert alert-danger").css('display','none');
                alertDiv.appendTo('body')
                alertDiv.append('<span>'+text+'</span>');
                elem = $(".bb-alert");
            }
        }
        elem.find("span").html(text);
        elem.delay(200).fadeIn().delay(4000).fadeOut();
    };

    return that;
}());

toastr.options = {
    "closeButton": true,
    "debug": false,
    "progressBar": false,
    "positionClass": "toast-top-right",
    "onclick": null,
    "showDuration": "400",
    "hideDuration": "1000",
    "timeOut": "7000",
//    "timeOut": "0",
    "extendedTimeOut": "1000",
    "showEasing": "swing",
    "hideEasing": "linear",
    "showMethod": "fadeIn",
    "hideMethod": "fadeOut"
};

/*
 $.ajaxSetup({
 type: 'POST',
 timeout: 30000,
 dataType: 'json'
 });
 */

$(document).bind('ajaxError', function(event,xhr,x) {
    var status = xhr.status;
    var statusText = xhr.statusText;
    var responseText = xhr.responseText;
    var msg = "获取服务端数据失败："+status+" - "+statusText+"<br>"+responseText;
    if (status == 0) {
        msg = '连接服务器失败';
    }
    toastr.warning(msg);
    //parent.layer.msg(msg);
}).bind('ajaxSend', function () {
    //layer.load(2, {shade: [0.1,'#fff']});
    layer.load(2);
}).bind('ajaxSuccess', function (event,xhr,options) {
    var responseJSON = xhr.responseJSON;
    if (responseJSON.success != undefined && responseJSON.success == false){
        toastr.warning("操作失败：<br>"+responseJSON.errorMsg);
    }
}).bind('ajaxComplete', function (event,xhr,options) {
    //var i = parent.layer.getFrameIndex();
    //parent.layer.close(i);
    parent.layer.closeAll();
});


(function() {

    $(document).ready(function() {

        /*
         # =============================================================================
         #   Navbar scroll animation
         # =============================================================================
         */

        $(".navbar.scroll-hide").mouseover(function() {
            $(".navbar.scroll-hide").removeClass("closed");
            return setTimeout((function() {
                return $(".navbar.scroll-hide").css({
                    overflow: "visible"
                });
            }), 150);
        });
        $(function() {
            var delta, lastScrollTop;
            lastScrollTop = 0;
            delta = 50;
            return $(window).scroll(function(event) {
                var st;
                st = $(this).scrollTop();
                if (Math.abs(lastScrollTop - st) <= delta) {
                    return;
                }
                if (st > lastScrollTop) {
                    $('.navbar.scroll-hide').addClass("closed");
                } else {
                    $('.navbar.scroll-hide').removeClass("closed");
                }
                return lastScrollTop = st;
            });
        });
        /*
         # =============================================================================
         #   Mobile Nav
         # =============================================================================
         */

        $('.navbar-toggle').click(function() {
            return $('body, html').toggleClass("nav-open");
        });


        /*
         # =============================================================================
         #   Timeline animation
         # =============================================================================
         */

        timelineAnimate = function(elem) {
            return $(".timeline.animated li").each(function(i) {
                var bottom_of_object, bottom_of_window;
                bottom_of_object = $(this).position().top + $(this).outerHeight();
                bottom_of_window = $(window).scrollTop() + $(window).height();
                if (bottom_of_window > bottom_of_object) {
                    return $(this).addClass("active");
                }
            });
        };
        timelineAnimate();
        $(window).scroll(function() {
            return timelineAnimate();
        });
        /*
         # =============================================================================
         #   Input placeholder fix
         # =============================================================================
         */

        if (!Modernizr.input.placeholder) {
            $("[placeholder]").focus(function() {
                var input;
                input = $(this);
                if (input.val() === input.attr("placeholder")) {
                    input.val("");
                    return input.removeClass("placeholder");
                }
            }).blur(function() {
                var input;
                input = $(this);
                if (input.val() === "" || input.val() === input.attr("placeholder")) {
                    input.addClass("placeholder");
                    return input.val(input.attr("placeholder"));
                }
            }).blur();
            return $("[placeholder]").parents("form").submit(function() {
                return $(this).find("[placeholder]").each(function() {
                    var input;
                    input = $(this);
                    if (input.val() === input.attr("placeholder")) {
                        return input.val("");
                    }
                });
            });
        }
    });

}).call(this);


/*
 (function(){
 var language = {
 "oAria": {
 "sSortAscending": ": 升序排列",
 "sSortDescending": ": 降序排列"
 },
 "sProcessing": "数据加载中",
 "sLengthMenu": "每页显示 _MENU_ 条",
 "sZeroRecords": "抱歉， 没有找到",
 "sInfo": "显示 _START_ 到 _END_ , 共 _TOTAL_ 条记录",
 "sInfoEmpty": "没有数据",
 "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
 "sZeroRecords": "没有检索到数据",
 "sSearch": "模糊查询:  ",
 emptyTable: "没有找到匹配的记录",
 "oPaginate": {
 "sFirst": "首页",
 "sPrevious": "上一页",
 "sNext": "下一页",
 "sLast": "尾页"
 }
 }
 $.fn.dataTable.defaults.language = language;
 //$.extend($.fn.dataTable.defaults.oLanguage,oLanguage)
 })();


 var DATA_TABLES = {
 DEFAULT_OPTION: { //DataTables初始化选项
 // language: {
 // },
 autoWidth: false,   //禁用自动调整列宽
 stripeClasses: ["odd", "even"],//为奇偶行加上样式，兼容不支持CSS伪类的场合
 order: [],          //取消默认排序查询,否则复选框一列会出现小箭头
 processing: true,  //隐藏加载提示,自行处理
 serverSide: true,   //启用服务器端分页
 searching: false,    //禁用原生搜索
 pagingType: "full_numbers",
 ordering: false
 },
 COLUMN: {
 CHECKBOX: { //复选框单元格
 className: "td-checkbox",
 orderable: false,
 width: "30px",
 data: null,
 render: function (data, type, row, meta) {
 return '<input type="checkbox" class="iCheck">';
 }
 }
 },
 RENDER: {   //常用render可以抽取出来，如日期时间、头像等
 ELLIPSIS: function (data, type, row, meta) {
 data = data || "";
 return '<span title="' + data + '">' + data + '</span>';
 }
 }
 }
 */

/** * 对Date的扩展，将 Date 转化为指定格式的String * 月(M)、日(d)、12小时(h)、24小时(H)、分(m)、秒(s)、周(E)、季度(q)
 可以用 1-2 个占位符 * 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字) * eg: * (new
 Date()).pattern("yyyy-MM-dd hh:mm:ss.S")==> 2006-07-02 08:09:04.423
 * (new Date()).pattern("yyyy-MM-dd E HH:mm:ss") ==> 2009-03-10 二 20:09:04
 * (new Date()).pattern("yyyy-MM-dd EE hh:mm:ss") ==> 2009-03-10 周二 08:09:04
 * (new Date()).pattern("yyyy-MM-dd EEE hh:mm:ss") ==> 2009-03-10 星期二 08:09:04
 * (new Date()).pattern("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
 */
Date.prototype.pattern=function(fmt) {
    var o = {
        "M+" : this.getMonth()+1, //月份
        "d+" : this.getDate(), //日
        "h+" : this.getHours()%12 == 0 ? 12 : this.getHours()%12, //小时
        "H+" : this.getHours(), //小时
        "m+" : this.getMinutes(), //分
        "s+" : this.getSeconds(), //秒
        "q+" : Math.floor((this.getMonth()+3)/3), //季度
        "S" : this.getMilliseconds() //毫秒
    };
    var week = {
        "0" : "/u65e5",
        "1" : "/u4e00",
        "2" : "/u4e8c",
        "3" : "/u4e09",
        "4" : "/u56db",
        "5" : "/u4e94",
        "6" : "/u516d"
    };
    if(/(y+)/.test(fmt)){
        fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    }
    if(/(E+)/.test(fmt)){
        fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "/u661f/u671f" : "/u5468") : "")+week[this.getDay()+""]);
    }
    for(var k in o){
        if(new RegExp("("+ k +")").test(fmt)){
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
        }
    }
    return fmt;
}

